﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// ArrayUtil 的摘要说明
/// </summary>
public class ArrayUtil
{
    /// <summary>
    /// DataTable 行列转换
    /// </summary>
    /// <param name="oldTable"></param>
    /// <returns></returns>
    public static DataTable ConvertDataTableToInverse(DataTable oldTable)
    {
        try
        {
            int h = oldTable.Rows.Count + 1;
            int l = oldTable.Columns.Count;
            DataTable newTable = new DataTable();
            for (int i = 0; i < h; i++)
            {
                if (i == 0)
                {
                    newTable.Columns.Add(oldTable.Columns[0].ToString());
                }
                else
                {
                    newTable.Columns.Add(oldTable.Rows[i - 1][0].ToString());
                }
            }

            for (int i = 0; i < l; i++)
            {
                DataRow r;
                r = newTable.NewRow();
                //oldTable.Columns.Count;
                for (int j = 0; j < h; j++)
                {
                    if (j == 0)
                    {
                        r[j] = oldTable.Columns[i].ToString();
                    }
                    else
                    {
                        r[j] = oldTable.Rows[j - 1][i].ToString();
                    }
                }
                if (i == 0)
                {
                    //
                }
                else
                {
                    newTable.Rows.Add(r);
                }
            }


            return newTable;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.ToString());
        }

    }

    /// <summary>
    /// DataTable To String[,]
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static string[,] ConvertDataTableToArray(DataTable dt)
    {
        if (dt == null) return null;
        string[,] ret = new string[dt.Rows.Count, dt.Columns.Count];
        int i = 0;
        foreach (DataRow row in dt.Rows)
        {
            int j = 0;
            foreach (DataColumn column in dt.Columns)
            {
                ret[i, j] = row[column.ColumnName].ToString();
                j++;
            }
            i++;
        }

        return ret;
    }

    /// <summary>
    /// DataTable To String[,]
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static string[,] ConvertDataTableToArrayWithTitle(DataTable dt)
    {
        if (dt == null) return null;
        string[,] ret = new string[dt.Rows.Count + 1, dt.Columns.Count];
        for (int k = 0; k < dt.Columns.Count; k++)
        {
            ret[0, k] = dt.Columns[k].ColumnName;
        }
        int i = 1;
        foreach (DataRow row in dt.Rows)
        {
            int j = 0;
            foreach (DataColumn column in dt.Columns)
            {
                ret[i, j] = row[column.ColumnName].ToString();
                j++;
            }
            i++;
        }

        return ret;
    }
}
